package com.example.pingze.service;

import com.example.pingze.entity.PoetUserCollect;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.repository.PoetUserCollectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class PoetUserCollectService {
    @Autowired
    private PoetUserCollectRepository repository;

    public PoetUserCollect collect(PoetUserCollect collect) {
        return repository.save(collect);
    }

    public List<PoetUserCollect> findPoetUserCollectByAccount(String account) {
        List<PoetUserCollect> find = repository.findPoetUserCollectByAccount(account);
        return find;
    }

    //通过账号和诗人名查找收藏信息
    public List<PoetUserCollect> findPoetUserCollect(PoetUserCollect collect) {
        Specification specification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!StringUtils.isEmpty(collect.getAccount())){
                    predicates.add(criteriaBuilder.like(root.get("account"), "%" + collect.getAccount() + "%"));
                }
                if(!StringUtils.isEmpty(collect.getPoetName())){
                    predicates.add(criteriaBuilder.like(root.get("poetName"), "%" + collect.getPoetName() + "%"));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return repository.findAll(specification);
    }

    public void delete(PoetUserCollect collect) {
        repository.delete(collect);
    }

    public PoetUserCollect findPoetUserCollectByAccountAndPoetName(String account, String name) {
        return repository.findPoetUserCollectByAccountAndPoetName(account, name);
    }

    public Page getList(PoetUserCollect poetUserCollect, Pageable pageable) {
        Specification<PoetUserCollect> specification=new Specification<PoetUserCollect>() {
            @Override
            public Predicate toPredicate(Root<PoetUserCollect> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!StringUtils.isEmpty(poetUserCollect.getId())){
                    predicates.add(criteriaBuilder.equal(root.get("id"),poetUserCollect.getId()));
                }
                if(!StringUtils.isEmpty(poetUserCollect.getAccount())){
                    predicates.add(criteriaBuilder.equal(root.get("account"),poetUserCollect.getAccount()));
                }
                if(!StringUtils.isEmpty(poetUserCollect.getPoetName())){
                    predicates.add(criteriaBuilder.equal(root.get("poetName"),poetUserCollect.getPoetName()));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page page = repository.findAll(specification,pageable);
        return page;
    }
    public PoetUserCollect add(String account,String poetName){
        if(StringUtils.isEmpty(account)){
            throw new PingZeException("账号不能为空");
        }
        if(StringUtils.isEmpty(poetName)){
            throw new PingZeException("收藏的诗人名字不能为空");
        }
        PoetUserCollect add=new PoetUserCollect();
        add.setId(UUID.randomUUID().toString());
        add.setAccount(account);
        add.setPoetName(poetName);
        repository.save(add);
        return add;
    }
    public PoetUserCollect edit(String id,String account,String poetName){
        PoetUserCollect updater= repository.findPoetUserCollectById(id);
        if(!updater.getAccount().equals(account)){
            updater.setAccount(account);
        }
        if(!updater.getPoetName().equals(poetName)){
            updater.setPoetName(poetName);
        }
        repository.save(updater);
        return  updater;
    }

    public PoetUserCollect findPoetUserCollectById(String id) {
        return repository.findPoetUserCollectById(id);
    }
}
